//
//  CustomCardView.m
//  CCDraggableCard-Master
//
//  Created by jzzx on 16/7/9.
//  Copyright © 2016年 Zechen Liu. All rights reserved.
//

#import "CustomCardView.h"

#import <UIImageView+WebCache.h>

@interface CustomCardView ()

@property (strong, nonatomic) UIImageView *imageView;
@property (strong, nonatomic) UIImageView *coverView;
@property (strong, nonatomic) UILabel *activity;

@property (strong, nonatomic) UIView *titleView;
@property (strong, nonatomic) UIImageView *roundImageView;
@property (strong, nonatomic) UIImageView *genderAge;
@property (strong, nonatomic) UILabel *name;
@property (strong, nonatomic) UIImageView *schoolImg;
@property (strong, nonatomic) UILabel *schoolLbl;


@property (strong, nonatomic) UIImageView *roundImageView2;
@property (strong, nonatomic) UIImageView *roundImageView3;

@property (strong, nonatomic) UIButton *totalBtn;

@property (assign, nonatomic) NSInteger attenderCount;

@end

@implementation CustomCardView

- (instancetype)init {
    if (self = [super init]) {
        [self loadComponent];
    }
    return self;
}

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self loadComponent];

    }
    return self;
}

- (void)loadComponent {
//    self.count = self.attenderCount;
    
    self.imageView = [[UIImageView alloc] init];
    self.coverView = [[UIImageView alloc]init];
    self.activity = [[UILabel alloc]init];
    self.titleView = [[UIView alloc]init];
    self.roundImageView = [[UIImageView alloc]init];
    
    self.imageView.contentMode = UIViewContentModeScaleAspectFill;
    [self.imageView.layer setMasksToBounds:YES];
    
    [self.activity setFont:[UIFont systemFontOfSize:46.0]];
    self.activity.textColor = [UIColor whiteColor];
    [self.activity sizeToFit];
    
    self.roundImageView.contentMode = UIViewContentModeScaleAspectFill;
    
    [self addSubview:self.imageView];
    [self addSubview:self.titleView];
    [self.imageView addSubview:self.coverView];
    [self.imageView addSubview:self.activity];
    [self.titleView addSubview:self.roundImageView];
    self.backgroundColor = [UIColor whiteColor];
}

- (void)cc_layoutSubviews {
    self.imageView.frame = CGRectMake(0, 0, self.frame.size.width, self.frame.size.width);
    self.coverView.frame = self.imageView.bounds;
    self.activity.center = CGPointMake(self.imageView.width * 0.5, self.imageView.height - self.activity.height * 0.5 - 6);
    self.titleView.frame = CGRectMake(0, self.imageView.bottom, self.frame.size.width, 91);
    
    self.roundImageView.frame = CGRectMake(10, 20, 50, 50);
    self.roundImageView.layer.cornerRadius = self.roundImageView.width * 0.5;
    self.roundImageView.layer.masksToBounds = YES;
    //单人卡片
    if (self.attenderCount == 1) {
        self.name.origin = CGPointMake(self.roundImageView.right + 10, 23);
        self.genderAge.center = CGPointMake(self.name.right + 5 + self.genderAge.width * 0.5, self.name.center.y);
        self.schoolImg.left = self.roundImageView.right + 10;
        self.schoolImg.bottom = self.roundImageView.bottom;
        self.schoolLbl.center = CGPointMake(self.schoolImg.right + 5 + self.schoolLbl.width * 0.5, self.schoolImg.center.y);
    }
    //多人卡片
    else {
        //两人
        self.roundImageView2.frame = CGRectMake(self.roundImageView.right + 8, self.roundImageView.top, 50, 50);
        //三人以上
        if (self.attenderCount > 2) {
            self.roundImageView3.frame = CGRectMake(self.roundImageView2.right + 8, self.roundImageView.top, 50, 50);
        }
        self.totalBtn.right = self.titleView.width - 6;
        self.totalBtn.bottom = self.titleView.height - 32;
    }
}

- (void)installData:(NSDictionary *)element {
    //异步获取
    
    NSLog(@"element%@", element);
    self.imageView.image = [UIImage imageNamed:@"default_head"];
    self.roundImageView.image  = [UIImage imageNamed:@"default_head"];
    
    [self.imageView sd_setImageWithURL:element[@"image"] placeholderImage:[UIImage imageNamed:@"default_head"]];
    [self.roundImageView sd_setImageWithURL:element[@"image"] placeholderImage:[UIImage imageNamed:@"default_head"]];

    self.coverView.image = [UIImage imageNamed:@"balck"];
    
    self.activity.text = element[@"activity"];
    [self.activity sizeToFit];
    
    NSArray *moreUsers = element[@"moreUsers"];
    NSString *moreUsersCount = element[@"moreUsersCount"];
    
    self.attenderCount = [moreUsersCount integerValue] + 1;
    
    if (moreUsers.count == 0) {
        self.attenderCount = 1;
    }
    
    self.count = self.attenderCount;
    
    //单人卡片
    if ([moreUsers count] == 0) {
        self.name = [[UILabel alloc] init];
        self.genderAge = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 30, 14)];
        self.schoolImg = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"card_school"]];
        self.schoolLbl = [[UILabel alloc]init];

        self.name.textColor = [UIColor colorWithRed:0x4a / 255.0 green:0x4a / 255.0 blue:0x4a / 255.0 alpha:1.0];
        self.name.font = [UIFont systemFontOfSize:16.0];
        NSString *name = [NSString stringWithFormat:@"%@", element[@"nickname"]];
        self.name.text = name;
        [self.name sizeToFit];
        
        NSString *sex = [NSString stringWithFormat:@"%@", element[@"sex"]];
        self.genderAge.image = [sex isEqualToString:@"1"] ? [UIImage imageNamed:@"boy"] : [UIImage imageNamed:@"girl"];


        NSString *birthday = [NSString stringWithFormat:@"%@", element[@"birthday"]];
        NSInteger age = ([[NSDate date] timeIntervalSince1970] - [birthday integerValue]) / (365 * 24 * 3600);
        
        UILabel *ageLbl = [[UILabel alloc]init];
        ageLbl.text = [NSString stringWithFormat:@"%li", (long)age];
        ageLbl.textColor = [UIColor whiteColor];
        ageLbl.font = [UIFont systemFontOfSize:10.0];
        [ageLbl sizeToFit];
        
        ageLbl.center = CGPointMake(self.genderAge.width - ageLbl.width * 0.5 - 3, self.genderAge.height * 0.5);
        
        self.schoolLbl.text = element[@"school"];
        self.schoolLbl.textColor = [UIColor colorWithRed:0x9b / 255.0 green:0x9b / 255.0 blue:0x9b / 255.0 alpha:1.0];
        self.schoolLbl.font = [UIFont systemFontOfSize:12.0];
        [self.schoolLbl sizeToFit];
        
        [self.genderAge addSubview:ageLbl];
        [self.titleView addSubview:self.schoolLbl];
        [self.titleView addSubview:self.schoolImg];
        [self.titleView addSubview:self.name];
        [self.titleView addSubview:self.genderAge];
    }
    //多人卡片
    else {
        //两人
        self.roundImageView2 = [[UIImageView alloc]init];
        
        self.roundImageView2.image = [UIImage imageNamed:@"default_head"];
        [self.roundImageView2 sd_setImageWithURL:moreUsers[0][@"headerImg"] placeholderImage:[UIImage imageNamed:@"default_head"]];
        
        self.roundImageView2.layer.cornerRadius = 25;
        self.roundImageView2.layer.masksToBounds = YES;
        [self.titleView addSubview:self.roundImageView2];
        //三人及以上
        if ([moreUsers count] == 2) {
            self.roundImageView3 = [[UIImageView alloc]init];
            self.roundImageView3.image = [UIImage imageNamed:@"default_head"];
            [self.roundImageView3 sd_setImageWithURL:moreUsers[1][@"headerImg"] placeholderImage:[UIImage imageNamed:@"default_head"]];
            self.roundImageView3.layer.cornerRadius = 25;
            self.roundImageView3.layer.masksToBounds = YES;
            [self.titleView addSubview:self.roundImageView3];
        }
        self.totalBtn = [[UIButton alloc]init];
        [self.totalBtn setTitle:[NSString stringWithFormat:@"...共%ld人", (long)self.attenderCount] forState:UIControlStateNormal];
        [self.totalBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [self.totalBtn.titleLabel setFont:[UIFont systemFontOfSize:10.0]];
        [self.totalBtn setBackgroundImage:[UIImage imageNamed:@"rectangle_black"] forState:UIControlStateNormal];
        [self.totalBtn setSize:CGSizeMake(44, 22)];
        [self.titleView addSubview:self.totalBtn];
    }
}

@end
